import unittest
import utils
import requests

from api.login import LoginApi
from api.tender import TenderApi
from parameterized import parameterized


class TestTender(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.login_api = LoginApi()
        cls.tender_api = TenderApi()

    def setUp(self) -> None:
        self.session = requests.Session()
        self.login_api.login(self.session, "13012345678", "test123")

    def tearDown(self) -> None:
        self.session.close()

    # 投资
    @parameterized.expand(utils.build_data("tender.json"))
    def test01_tender(self, desc, id, amount, status_code, status, description):
        print(f"desc={desc} id={id} amount={amount} status_code={status_code} "
              f"status={status} description={description}")
        # 测试数据
        # id = 2059
        # amount = 100

        # 调用接口
        r = self.tender_api.tender(self.session, id, amount)
        print("tender data=", r.json())

        # 断言
        utils.common_assert(self, r, status_code, status, description)

    # 调用第三方投资接口
    def test02_tender_three(self):
        # 测试数据
        id = 2059
        amount = 100

        # 调用接口
        r = self.tender_api.tender(self.session, id, amount)
        print("tender data=", r.json())

        # 断言
        utils.common_assert(self, r, 200, 200, None)

        # 提取form数据
        html = r.json().get("description").get("form")
        action, form_data = utils.parse_html(html)

        # 调用第三方接口
        r2 = requests.post(action, data=form_data)
        print("第三方投资接口返回数据：", r2.text)
        self.assertEqual(200, r2.status_code)
        self.assertIn("OK", r2.text)
